
function getType(v){
    if(v.indexOf('//')>=0&&!(v.substring(0,v.indexOf('//')).indexOf(';'))) return 0;
    if(!v.replace(/ /g,"")) return 0;
    if(v.indexOf('message')>=0) return "message";
    if(v.indexOf('required')>=0) return "required";
    if(v.indexOf('optional')>=0) return "optional";
    if(v.indexOf('repeated')>=0) return "repeated";
    if(v.indexOf('}')>=0) return "end";
}

function getMesssageName(v){
    var index=v.indexOf('message');
    var i=v.lastIndexOf("{")
    var name=v.substring(index+8,i!=-1?(i-1):v.length);
    name=name.replace(/ /g,"").replace(/\n/g,"").replace(/\r/g,"");
    return name;
}

function getFiled(v){
    var index=v.indexOf('=');
    var front=v.substring(0,index);
	front=front.replace(/\t/g," ");
	front=front.split(' ');
    var behind=v.substring(index+1,v.lastIndexOf(";"));
	
    for(var i=0;i<front.length;i++){
        if(!front[i].replace(/ /g,"")){
            front.splice(i,1);
            i--;
        }
    }

    var index=undefined;
    var defaultValue=undefined;
   
    if(behind.indexOf('[')>0){
        index=parseInt(behind.substring(0,behind.indexOf('[')).replace(/ /g,""));
        defaultValue=(behind.substring(behind.indexOf('[')+1,behind.indexOf(']')).split('=')[1]).replace(/ /g,"");
    }else{
        index=parseInt(behind);
    }
    return {
        type:front[0].replace(/\n/g,"").replace(/\r/g,"").replace(/\t/g,""),
        propertyType:front[1].replace(/\n/g,"").replace(/\r/g,"").replace(/\t/g,""),
        propertyName:front[2].replace(/\n/g,"").replace(/\r/g,"").replace(/\t/g,""),
        index:index,
        default:defaultValue?defaultValue.replace(/\n/g,"").replace(/\r/g,"").replace(/\t/g,""):defaultValue};
}

function parse(cells){
    var structs=[];
    var struct;
    while(cells.length){
        var cell=cells.shift();
        var type=getType(cell);

        if(type==0) continue;
        
        switch(type){
            case "message":
                if(!struct){
                    struct={name:"",fileds:[],messages:[],opened:false};
                    struct.name=getMesssageName(cell);
                    struct.opened=true;
                }else{
                    var layer=1;
                    var childCells=[cell];
                    var type=getType(cell)
                    while(type!="end"||layer!=0){
                        cell=cells.shift();
                        type=getType(cell);
                        childCells.push(cell);
                        if(type=='message') layer++;
                        if(type=='end') layer--;
                    }
                    struct.messages=struct.messages.concat(parse(childCells));
                }
            break;
            case "required":
            case "optional":
            case "repeated":
                struct.fileds.push(getFiled(cell));
            break;
            case "end":
                struct.opened=false;
                structs.push(struct);
                struct=null;
            break;
        }
    }
    return structs;
}

module.exports=function(contents){
    for(var content of contents){
        content.structs=parse(content.content.split('\n'));
    }
    return contents;
}